package com.lenovo.smarttraffic.database.dao;

import android.content.Context;


/**
 * author  张猛
 * Test   GreenDao数据库   与  retrofit2   之间的联动
 *
 *  不能在  GSON  类里面  直接使用 GreenDao
 *
 *
 **/
public class DaoManager_Web {
    private Context mContext;
    private static final String DB_NAME = "web.db";
    private volatile static DaoManager_Web mInstance_web;
    private static DaoMaster mDaoMaster_web;
    private static DaoMaster.DevOpenHelper mhelper_web;
    private static DaoSession mDaoSession_web;

    public DaoManager_Web() {

    }

    public static DaoManager_Web getInstance() {
        if (mInstance_web == null) {
            synchronized (DaoManager_Web.class) {
                if (mInstance_web == null) {
                    mInstance_web = new DaoManager_Web();
                }

            }
        }
        return mInstance_web;
    }

    public void init(Context context) {
        this.mContext = context;
    }

    public DaoMaster getDaoMaster_web() {
        if (mDaoMaster_web == null) {
            mhelper_web = new DaoMaster.DevOpenHelper(mContext, DB_NAME);
            mDaoMaster_web = new DaoMaster(mhelper_web.getWritableDatabase());
        }
        return mDaoMaster_web;
    }

    public DaoSession getDaoSession() {
        if (mDaoSession_web == null) {
            if (mDaoMaster_web == null) {
                mDaoMaster_web = getDaoMaster_web();
            }
            mDaoSession_web = mDaoMaster_web.newSession();
        }
        return mDaoSession_web;

    }

    public void closeConnection() {
        closeHelper();
        closeDaoSession();
    }

    public void closeHelper() {
        if (mhelper_web != null) {
            mhelper_web.close();
            mhelper_web = null;
        }
    }

    public void closeDaoSession() {
        if (mDaoSession_web != null) {
            mDaoSession_web.clear();
            mDaoSession_web = null;
        }
    }


}
